package com.xjrsoft.module.workflow.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjrsoft.core.tool.utils.CollectionUtil;
import com.xjrsoft.core.tool.utils.StringUtil;
import com.xjrsoft.module.workflow.entity.XjrNwfSchemeauth;
import com.xjrsoft.module.workflow.mapper.XjrNwfSchemeauthMapper;
import com.xjrsoft.module.workflow.service.IXjrNwfSchemeAuthService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class XjrNwfSchemeAuthServiceImpl extends ServiceImpl<XjrNwfSchemeauthMapper, XjrNwfSchemeauth> implements IXjrNwfSchemeAuthService {

    /**
     * @Author:湘北智造-框架开发组
     * @Date:2020/10/26
     * @Param:[schemeInfoId]
     * @return:java.util.List<com.xjrsoft.module.workflow.entity.XjrNwfSchemeauth>
     * @Description:根据流程模板信息Id查询流程模板权限
     */
    @Override
    public List<XjrNwfSchemeauth> getBySchemeInfoId(String schemeInfoId) {
        return baseMapper.selectList(new QueryWrapper<XjrNwfSchemeauth>().eq("F_SchemeInfoId", schemeInfoId));
    }

    /**
     * @Author:湘北智造-框架开发组
     * @Date:2020/10/26
     * @Param:[schemeInfoId]
     * @return:boolean
     * @Description:删除流程模板权限
     */
    @Override
    public boolean delete(String schemeInfoId) {
        List<XjrNwfSchemeauth> list = getBySchemeInfoId(schemeInfoId);
        for (XjrNwfSchemeauth xjrNwfSchemeauth : list) {
            if (baseMapper.deleteById(xjrNwfSchemeauth.getId()) < 0) {
                return false;
            }
        }
        return true;
    }

    @Override
    @Transactional
    public boolean saveAuthList(String nwfSchemeInfoId, JSONArray jsonArray) throws Exception {
        // 删除之前的流程权限列表
        List<XjrNwfSchemeauth> authList = getBySchemeInfoId(nwfSchemeInfoId);
        List<String> ids = authList.stream().map(XjrNwfSchemeauth::getId).collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(ids)) {
            if (!this.removeByIds(ids)) {
                return false;
            }
        }
        ArrayList<XjrNwfSchemeauth> xjrNwfSchemeauths = new ArrayList<>();
        // 新增
        boolean saveBatch = true;
        if (CollectionUtil.isNotEmpty(jsonArray)) {
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                XjrNwfSchemeauth xjrNwfSchemeauth = new XjrNwfSchemeauth();
                xjrNwfSchemeauth.setSchemeinfoId(nwfSchemeInfoId);
                xjrNwfSchemeauth.setObjName(jsonObject.getString("F_ObjName"));
                xjrNwfSchemeauth.setObjId(jsonObject.getString("F_ObjId"));
                xjrNwfSchemeauth.setObjType(Integer.valueOf(jsonObject.getString("F_ObjType")));
                xjrNwfSchemeauths.add(xjrNwfSchemeauth);
            }
            saveBatch = this.saveBatch(xjrNwfSchemeauths);
        }
        return saveBatch;
    }
}
